package com.jintian.smart.kernel.security.service;

import com.jintian.smart.kernel.common.util.DateUtil;
import com.jintian.smart.kernel.common.util.SmartBeanUtil;
import com.jintian.smart.kernel.common.util.StringUtil;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.kernel.orm.impl.AbstractEntityServiceImpl;
import com.jintian.smart.base.api.system.entity.ChangeLogEntity;
import com.jintian.smart.base.api.system.form.ChangeLogAddForm;
import com.jintian.smart.base.api.system.form.ChangeLogQueryForm;
import com.jintian.smart.base.api.system.form.ChangeLogUpdateForm;
import com.jintian.smart.base.api.system.view.ChangeLogVO;
import com.jintian.smart.kernel.security.mapper.ChangeLogMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统更新日志 Service
 */
@Service
public class ChangeLogService extends AbstractEntityServiceImpl<ChangeLogMapper, ChangeLogEntity> {
    /**
     * 分页查询
     */
    public PageResult<ChangeLogVO> queryPage(ChangeLogQueryForm queryForm) {
        LambdaQuery<ChangeLogEntity> query = this.getEntityMapper().createLambdaQuery();
        if (queryForm.getType() != null && queryForm.getType() > 0) {
            query.andEq(ChangeLogEntity::getType, queryForm.getType());
        }
        if (StringUtil.isNotBlank(queryForm.getKeyword())) {
            LambdaQuery<ChangeLogEntity> subQuery = query.condition();
            subQuery.orLike(ChangeLogEntity::getUpdateVersion, "%" + queryForm.getKeyword() + "%");
            subQuery.orLike(ChangeLogEntity::getPublishAuthor, "%" + queryForm.getKeyword() + "%");
            subQuery.orLike(ChangeLogEntity::getContent, "%" + queryForm.getKeyword() + "%");
        }

        if (queryForm.getPublicDateBegin() != null) {
            query.andGreatEq(ChangeLogEntity::getPublicDate, DateUtil.toStartOfDay(queryForm.getPublicDateBegin()));
        }

        if (queryForm.getPublicDateEnd() != null) {
            query.andLessEq(ChangeLogEntity::getPublicDate, DateUtil.toEndOfDay(queryForm.getPublicDateEnd()));
        }

        if (queryForm.getCreateTime() != null) {
            query.andGreatEq(ChangeLogEntity::getCreateTime, DateUtil.toStartOfDay(queryForm.getCreateTime()));
            query.andLessEq(ChangeLogEntity::getCreateTime, DateUtil.toEndOfDay(queryForm.getCreateTime()));
        }

        if (StringUtil.isNotBlank(queryForm.getLink())) {
            query.andEq(ChangeLogEntity::getLink, queryForm.getLink());
        }

        query.order(ChangeLogEntity::getUpdateVersion, false);
        PageResult<ChangeLogEntity> page = query.page(queryForm.getPageNo(), queryForm.getPageSize());
        PageResult<ChangeLogVO> pageResult = SmartBeanUtil.copyPage(page, ChangeLogVO.class);
        return pageResult;
    }

    /**
     * 添加
     */
    public synchronized ResponseDTO<String> add(ChangeLogAddForm addForm) {
        ChangeLogEntity existVersion = this.selectByVersion(addForm.getUpdateVersion());
        if (existVersion != null) {
            return ResponseDTO.userErrorParam("此版本已经存在");
        }

        ChangeLogEntity changeLogEntity = SmartBeanUtil.copy(addForm, ChangeLogEntity.class);
        this.getEntityMapper().insert(changeLogEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     */
    public synchronized ResponseDTO<String> update(ChangeLogUpdateForm updateForm) {
        ChangeLogEntity existVersion = this.selectByVersion(updateForm.getUpdateVersion());
        if (existVersion != null && !updateForm.getChangeLogId().equals(existVersion.getChangeLogId())) {
            return ResponseDTO.userErrorParam("此版本已经存在");
        }
        ChangeLogEntity changeLogEntity = SmartBeanUtil.copy(updateForm, ChangeLogEntity.class);
        this.getEntityMapper().updateById(changeLogEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public synchronized ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }
        this.deleteByIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public synchronized ResponseDTO<String> delete(Long changeLogId) {
        if (null == changeLogId) {
            return ResponseDTO.ok();
        }

        this.deleteById(changeLogId);
        return ResponseDTO.ok();
    }

    public ChangeLogVO getById(Long changeLogId) {
        return SmartBeanUtil.copy(this.getById(changeLogId), ChangeLogVO.class);
    }

    /**
     * 根据版本查询 ChangeLog
     */
    public ChangeLogEntity selectByVersion(String version) {
        ChangeLogEntity changeLogEntity = this.getEntityMapper().createLambdaQuery().andEq(ChangeLogEntity::getUpdateVersion, version).single();
        return SmartBeanUtil.copy(changeLogEntity, ChangeLogEntity.class);
    }
}
